﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;


/// <summary>
///Database 的摘要说明
/// </summary>
public class AccessDatabase
{
    /// <summary>
    /// 数据库连接 
    /// </summary>
    private static OleDbConnection dbconn;

    /// <summary>
    /// 构造函数
    /// </summary>
    public AccessDatabase()
    {
        CreateConn();
    }

    /// <summary>
    /// 创建数据库连接
    /// </summary>
    /// <returns>返回数据库连接</returns>
    public OleDbConnection CreateConn()
    {
        string dbName = ConfigurationSettings.AppSettings["connstring"].ToString();
        string dbPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/" + dbName);
        try
        {
            if (dbconn == null)
            {

                dbconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath);
                dbconn.Open();

            }
            else if (dbconn.State == ConnectionState.Closed)
            {
                dbconn.Open();
            }
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
        return dbconn;
    }

    /// <summary>
    /// 执行sql语句，返回DataReader,在.net中 DataReader 类似于java中的ResultSet
    /// </summary>
    /// <param name="cmdstr">sql语句</param>
    /// <returns>OleDbDataReader</returns>
    public OleDbDataReader ExeSql(string cmdstr)
    {
        try
        {
            OleDbCommand sqlcmd = new OleDbCommand(cmdstr, CreateConn());
            return sqlcmd.ExecuteReader();
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
    }
    //重载
    public OleDbDataReader ExeSql(string cmdstr, OleDbParameter[] parameters)
    {
        try
        {
            OleDbCommand sqlcmd = CreateCmdbyParmeters(cmdstr, parameters);
            return sqlcmd.ExecuteReader();
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
    }

    /// <summary>
    /// 执行带参数的sql语句
    /// </summary>
    /// <param name="cmdstr">sql语句</param>
    /// <param name="parameters">参数数组</param>
    /// <returns>OleDbDataReader</returns>
    public OleDbDataReader ExeSqlByParameters(string cmdstr, OleDbParameter[] parameters)
    {
        try
        {
            return CreateCmdbyParmeters(cmdstr, parameters).ExecuteReader();
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
    }

    /// <summary>
    /// 返回所执行sql语句的首行首列
    /// </summary>
    /// <param name="cmdstr"></param>
    /// <returns></returns>
    public object ExeReturnOne(string cmdstr)
    {
        try
        {
            OleDbCommand sqlcmd = new OleDbCommand(cmdstr, CreateConn());
            return sqlcmd.ExecuteScalar();
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
    }
    //重载
    public object ExeReturnOne(string cmdstr, OleDbParameter[] parameters)
    {
        try
        {
            OleDbCommand sqlcmd = CreateCmdbyParmeters(cmdstr, parameters);
            return sqlcmd.ExecuteScalar();
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
    }

    /// <summary>
    /// 返回执行sql语句是否成功
    /// </summary>
    /// <param name="cmdstr"></param>
    /// <returns></returns>
    public bool ExeReturnbool(string cmdstr)
    {
        try
        {
            OleDbCommand sqlcmd = new OleDbCommand(cmdstr, CreateConn());
            int execount = sqlcmd.ExecuteNonQuery();
            if (execount > 0) { return true; }
            else { return false; }
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
    }

    //重载
    public bool ExeReturnbool(string cmdstr, OleDbParameter[] parameters)
    {
        try
        {
            OleDbCommand sqlcmd = CreateCmdbyParmeters(cmdstr, parameters);
            int execount = sqlcmd.ExecuteNonQuery();
            if (execount > 0) { return true; }
            else { return false; }
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
    }

    /// <summary>
    /// 传入sql语句和参数构建command
    /// </summary>
    /// <param name="cmdstr">sql</param>
    /// <param name="parameters">参数数组</param>
    /// <returns>构建成功的Command</returns>
    private OleDbCommand CreateCmdbyParmeters(string cmdstr, OleDbParameter[] parameters)
    {
        try
        {
            OleDbCommand oleCmd = new OleDbCommand(cmdstr, CreateConn());
            foreach (OleDbParameter parmeter in parameters)
            {
                oleCmd.Parameters.Add(parmeter);
            }

            return oleCmd;
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
    }

    /// <summary>
    /// 创建参数
    /// </summary>
    /// <param name="parameterName">参数名</param>
    /// <param name="parmeterValue">参数值</param>
    /// <returns>参数</returns>
    public OleDbParameter createParamter(string parameterName, object parmeterValue)
    {
        return new OleDbParameter(parameterName, parmeterValue);
    }

}